﻿/*
///    This file is part of BannerLib4Net.
///
///    BannerLib4Net is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 3 of the License, or
///    (at your option) any later version.

///    BannerLib4Net is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with BannerLib4Net.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BannerLib4Net.Web.Session
{
    [Serializable]
    public class SessionToken: Hashtable
    {
        public SessionToken()
        {
           
        }

        /// <summary>
        /// Returns the user stored in the session hashtable
        /// </summary>
        public string User
        {
            get
            {
                string retval = string.Empty;

                if (this["user.name"] != null)
                {
                    retval = this["user.name"].ToString();
                }

                return retval;
            }
            set
            {
                this["user.name"] = value;
            }
        }


        public String Email
        {
            get
            {
                string retval = string.Empty;

                if (this["user.email"] != null)
                {
                    retval = this["user.email"].ToString();
                }

                return retval;
            }
            set
            {
                this["user.email"] = value;
            }
        }


        /// <summary>
        /// Private utility method that checks to see if 
        /// the role is in the comma delimited list that 
        /// is stored in session
        /// </summary>
        public bool CheckRoleMembership(String searchRole)
        {
            bool retval = false;

            if (this["roles"] != null && !String.IsNullOrEmpty(this["roles"].ToString()))
            {
                if (this["roles"].ToString().IndexOf(",") > 0)
                {
                    String[] tempRoles = this["roles"].ToString().Split(',');

                    if (tempRoles.Length > 1)
                    {
                        foreach (var s in tempRoles)
                        {
                            if (s.Trim() == searchRole)
                            {
                                retval = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        retval = tempRoles[0].Equals(searchRole);
                    }

                }
                else
                {
                    retval = this["roles"].Equals(searchRole);
                }

            }

            return retval;
        }

        public String Roles
        {
            get
            {
                return this["roles"].ToString();
            }
            set
            {
                this["roles"] = value;
            }
        }
    }
}
